package com.uphone.magicleafteacher.pro.activity.welcome;


import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.radish.baselibrary.Intent.IntentUtils;
import com.radish.baselibrary.dialog.RadishDialog;
import com.radish.baselibrary.utils.ActivityCollector;
import com.uphone.magicleafteacher.MainActivity;
import com.uphone.magicleafteacher.R;
import com.uphone.magicleafteacher.base.mvp.BaseMvpActivity;
import com.uphone.magicleafteacher.base.mvp.BaseView;
import com.uphone.magicleafteacher.pro.fragment.home.HomeFragment;
import com.uphone.magicleafteacher.util.StringUtils;
import com.uphone.magicleafteacher.view.wheelview.WheelView;
import com.uphone.magicleafteacher.vo.LoginBean;

import java.util.ArrayList;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.ResponseBody;

/**
 * Created by hzy on 2019/1/18
 * WelcomeActivity  打开app第一页
 *
 * @author hzy
 */
public class WelcomeActivity extends BaseMvpActivity<WelcomePresenter> implements WelcomeContract.View {
    @Inject
    WelcomePresenter mPresenter;
    @BindView(R.id.edit_account)
    EditText editAccount;
    @BindView(R.id.edit_password)
    EditText editPassword;
    private SharedPreferences preferences;

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    public void updateView(ResponseBody responseBean, int status) {
        if (status == 1) {
            finish();
        }
    }

    //返回的数据
    @Override
    public void updateView(Object t) {
        if (t instanceof LoginBean) {
            LoginBean loginBean = (LoginBean) t;
            String msg = loginBean.getMsg();
            Log.e("TAG", msg + "");
            int teacherId = loginBean.getTokenInfo().getTeacherId();
            String token = loginBean.getTokenInfo().getToken();
            SharedPreferences.Editor editor = getSharedPreferences("moye", MODE_PRIVATE).edit();
            editor.putString("loginName", editAccount.getText().toString());
            editor.putString("password", editPassword.getText().toString());
            editor.putString("teacherId", teacherId + "");
            editor.putInt("moye", 1);//修改数据
            editor.putString("token", token);
            editor.apply();

            Log.e("TAG", token);
            ActivityCollector.removeAll();
            IntentUtils.getInstance().with(this, MainActivity.class).start();

        }


    }

    @Override
    protected void initBundle() {

    }

    @Override
    protected int initLayout() {
        return R.layout.activity_welcome;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initTitle() {

    }

    @Override
    protected void initData() {
        preferences = getSharedPreferences("moye", MODE_PRIVATE);
        int moye = preferences.getInt("moye", 0);
        if (moye > 0) {
            //如果不是第一次登录，直接跳转到首页
            Intent intent = new Intent();
            intent.setClass(this, MainActivity.class);
            ActivityCollector.removeAll();
            startActivity(intent);
            return;
        }
    }

    @Override
    protected void initListener() {

    }

    private ArrayList getNumbers() {
        ArrayList<String> list = new ArrayList<>();
        for (int i = 1; i < 100; i++) {
            list.add(i + "");
        }
        return list;
    }

    @OnClick({R.id.bt_entry_test})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bt_entry_test:
                if (StringUtils.isEmpty(editAccount.getText().toString())) {
                    Toast.makeText(this, "请输入账号", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (StringUtils.isEmpty(editPassword.getText().toString())) {
                    Toast.makeText(this, "请输入密码", Toast.LENGTH_SHORT).show();
                    return;
                }
                onLogin(editAccount.getText().toString(), editPassword.getText().toString());
                Log.e("TAG", editAccount.getText().toString());

//                IntentUtils.getInstance().with(this, MainActivity.class).start();
//                finish();
                break;


        }
    }

    private void onLogin(String loginName, String password) {
        loginName = editAccount.getText().toString();
        password = editPassword.getText().toString();
        Log.e("TAG", loginName.toString());
        mPresenter.attachView(this);
        mPresenter.postLogin(loginName, password);

    }


}
